//
//  labelManager.m
//  Void Dead
//
//  Created by Sidney Just on 25.08.09.
//  Copyright 2009 Sidney Just
//  This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; 
//  either version 3 of the License, or (at your option) any later version.
//  
//  This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty 
//  of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
//  

#import "labelManager.h"
#import "label.h"
#import "Engine.h"

@implementation VD_labelManager

- (id)initWithEngine:(VD_Engine *)eng {
	if(self = [super init])
	{
		count = 0;
		regLabels = (VD_label **)malloc(sizeof(VD_label *));
		render_view = eng;
	}
	return self;
}

- (void)dealloc {
	free(regLabels);
	
	[super dealloc];
}

- (void)drawAll {
	polyCount = 0;
	drawCalls = 0;
	
	if(count > 0)
	{
		glEnable(GL_BLEND);
		glEnable(GL_TEXTURE_2D);
		glEnableClientState(GL_TEXTURE_COORD_ARRAY);	
		glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
		
		for(int i=0; i<count; i++)
		{
			if(regLabels[i].visible == YES)
			{
				glColor4f(regLabels[i].textColor.red, regLabels[i].textColor.blue, regLabels[i].textColor.green, regLabels[i].textColor.alpha);
		
				[regLabels[i] drawLabel:render_view];
				polyCount += 2;
				drawCalls += 1;
			}
		}
		
		render_view.visiblePolygons += polyCount;
		render_view.drawCalls += drawCalls;
	}
}

- (VD_label *)createLabel {
	count += 1;
	regLabels = (VD_label **)realloc(regLabels, count*sizeof(VD_label *));
	
	regLabels[count-1] = [[VD_label alloc] init];
	return regLabels[count-1];
}

- (void)deleteLabel:(VD_label *)lbl {
	int i;
	for(i=0; i<count; i++)
	{
		if(lbl == regLabels[i])
		{
			[lbl release];
			
			for(int j=i; j<count-1; j++)
			{
				regLabels[j] = regLabels[j+1];
			}
			
			count -= 1;
			regLabels = (VD_label **)realloc(regLabels, count*sizeof(VD_label *));
			
			break;
		}
	}
}

@end
